package com.example.yian.controller;

import com.example.yian.entity.TextSearchRequest;
import com.example.yian.entity.TextSearchResponse;
import com.example.yian.service.TextSearchService;
import com.example.yian.service.WeChatAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@RestController
public class TextSearchController {

    @Autowired
    private TextSearchService textSearchService;

    @Autowired
    private WeChatAuthService weChatAuthService;

    @PostMapping("/textSearch")
    public Map<String, Object> handleTextSearch(@RequestHeader("Authorization") String token, @RequestBody Map<String, String> requestBody) {
        Map<String, Object> response = new HashMap<>();
        Optional<Long> userIdOpt = weChatAuthService.getUserIdFromToken(token);
        if (userIdOpt.isEmpty()) {
            response.put("code", 401);
            response.put("msg", "登录过期");
            response.put("time", Instant.now().toEpochMilli());
            response.put("data", new HashMap<>());
            return response;
        }
        String textMessage = requestBody.get("text_message");
        Boolean reChat = false;
        Boolean stream = false;
        // 创建请求记录
        TextSearchRequest request = textSearchService.createRequest(textMessage, reChat, stream, userIdOpt.get());
        String chatId = "5261ef24-1cf8-11ef-bbae-0242ac110002";
        // 调用第三方接口并获取应答数据
        Map<String, Object> thirdPartyResponse = textSearchService.callThirdPartyApi(chatId, textMessage, reChat, stream);
        Map<String, Object> data = (Map<String, Object>) thirdPartyResponse.get("data");

        // String chatId = (String) data.get("chat_id");
        String textId = (String) data.get("id");
        Boolean operate = (Boolean) data.get("operate");
        String content = (String) data.get("content");
        Boolean isEnd = (Boolean) data.get("is_end");
        String refText = "GB50310电梯工程施工质量验收"; // 示例引用文本
        // 创建应答记录
        TextSearchResponse textSearchResponse = textSearchService.createResponse(request.getId(), chatId, textId, operate, content, refText, isEnd);
        response.put("code", 200);
        response.put("message", "成功");
        response.put("time", Instant.now().toEpochMilli());
        response.put("data", textSearchResponse);
        return response;
    }

    @PostMapping("/textContent")
    public Map<String, Object> getTextContentByTokenAndTextId(
            @RequestHeader("Authorization") String token,
            @RequestBody Map<String, String> requestBody) {

        Map<String, Object> response = new HashMap<>();

        // 验证token有效性
        Optional<Long> userIdOpt = weChatAuthService.getUserIdFromToken(token);
        if (userIdOpt.isEmpty()) {
            response.put("code", 401);
            response.put("msg", "登录过期");
            response.put("time", Instant.now().toEpochMilli());
            response.put("data", new HashMap<>());
            return response;
        }
        // 从请求体中获取textId
        String textId = requestBody.get("textId");

        // 根据textId查询content和refText
        Optional<TextSearchResponse> textSearchResponseOpt = textSearchService.findByTextId(textId);

        if (textSearchResponseOpt.isPresent()) {
            TextSearchResponse textSearchResponse = textSearchResponseOpt.get();
            Map<String, Object> data = new HashMap<>();
            data.put("content", textSearchResponse.getContent());
            data.put("refText", textSearchResponse.getRefText());

            response.put("code", 200);
            response.put("message", "查询成功");
            response.put("time", Instant.now().toEpochMilli());
            response.put("data", data);
        } else {
            response.put("code", 404);
            response.put("msg", "未找到对应内容");
            response.put("time", Instant.now().toEpochMilli());
            response.put("data", new HashMap<>());
        }

        return response;
    }

}
